<div id='sidebar' class='ui-widget-content ui-corner-bottom'>
	<h2 class='ui-widget-header'>CMS Users</h2>
	<div id='sidebar_content'>
		<ul id='user_tree' class='tree'>
			<?php foreach ($this->users as $role => $users) {?>
				<li class='role' id='<?= $role; ?>' rel="role"><a><ins></ins><?= $role; ?></a>
				<?php if(count($users) > 0) {?>
					<ul>
    					<?php foreach ($users as $user) {?>
    						<li id='<?= $user['id']?>' rel="user" class='user'><a href='#' rel='<?= $user['id']?>'><ins></ins><?= $user['first_name'] . ' ' . $user['last_name']?></a></li>
    					<?php } // end for each user?>
					</ul>
					<?php } // end if role has users?>
				</li>
            <?php } // end for each roles ?>
        </ul>
	</div>
</div>
<div id='main_form'>
	<h1>Manage Users</h1>
	<p>Intro</p>
</div>

<?php
$this->jQuery()->addJavascriptFile('/jquery/jquery.tree.js');
$this->jQuery()->addJavascriptFile('/jquery/jquery.cookie.js');
$this->jQuery()->addJavascriptFile('/jquery/plugins/jquery.tree.cookie.js');
$this->jQuery()->addJavascriptFile('/jquery/plugins/jquery.tree.contextmenu.js');
$this->jQuery()->addJavascriptFile('/jquery/plugins/jquery.tree.themeroller.js');
$this->jQuery()->onLoadCaptureStart()?>
var tree = $("#sidebar_content").tree({
    rules : {
            // only nodes of type root can be top level nodes
            valid_children : [ "role" ]
    },
    types: {
        "role": {
            deletable: false,
            draggable: false,
            renamable: false
         },
         "user": {
            max_children : 0
         }
        },
        lang: {
            new_node	: "New User"
        },
        ui: {theme_name: 'digitalus_tree'},
        plugins : {
            contextmenu : {
                items : {
                        // we need to remove these and then add them with a new alias to sort them
                        //todo rewrite the context menu to support sorting
                        rename: false,
                        create: false,
                        remove: false,
                        edit_item	:	{
                            label	: "Edit User",
                            icon	: "edit_user",
                            visible	: function (NODE, TREE_OBJ) {
                                    if(NODE.length != 1) return 0;
                                    if($(NODE).hasClass("role")) {
                                            return false;
                                    }
                            },
                            action	: function (NODE, TREE_OBJ) {
                                    $.dig.ui.disable();
                                    var userId = NODE.attr('id');
                                    $('#main_form').load('/digUser/account/form/id/' + userId, null, function(){
                                             $.dig.ui.enable();
                                              $.dig.ui.init();
                                              $.dig.editor.init();
                                              initForm();
                                    });
                            },
                            separator_after : true

                                    },
                    remove_item : {
                            label	: "Delete",
                            icon	: "remove_user",
                            visible	: function (NODE, TREE_OBJ) {
                                    if($(NODE).hasClass("role")) {
                                            return false;
                                    }
                                    var ok = true;
                                    $.each(NODE, function () {
                                            if(TREE_OBJ.check("deletable", this) == false) {
                                                    ok = false;
                                                    return false;
                                            }
                                    });
                                    return ok;
                            },
                            action	: function (NODE, TREE_OBJ) {
                                    var userId = NODE.attr('id');
                                    var userName = NODE.find('a:first').text();
                                    if(confirm("Are you sure you want to permanently delete " + userName + "?")) {
                                        $.post('/digUser/account/delete/id/' + userId, null, function(){
                                            $.each(NODE, function () {
                                                    TREE_OBJ.remove(this);
                                            });
                                        });
                                    }
                            },
                            separator_after : true
                    },
                    create_item : {
                            label	: "Add User",
                            icon	: "create_user",
                            visible	: function (NODE, TREE_OBJ) {
                                    if($(NODE).hasClass("user")) {
                                            return false;
                                    }
                                    if(NODE.length != 1) return 0;
                                    return TREE_OBJ.check("creatable", NODE);
                            },
                            action	: function (NODE, TREE_OBJ) {
                                    TREE_OBJ.create({new_name: 'tester'}, TREE_OBJ.get_node(NODE[0]));
                            }
                    }
                        }
                }
        },
        callback: {
                onmove: function(NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
                        var data = {
                                id	 : $(NODE).attr('id'),
                                role : $(NODE).closest('.role').attr('id')
                        }
                    $.post('/digUser/account/set-role', data, function(){
                        // so something
                    }, 'json');
                },
                oncreate: function(NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
                    // first create a new blank user
                    $.post('/digUser/account/create', null, function(response){
                        $(NODE).attr('id', response);
                        // set the users role
                        var role = $(NODE).closest('li.role').attr('id');
                        $.post('/digUser/account/set-role', {id: response, role: role});
                    }, 'json');
                return false;
                },
                onrename: function(NODE, LANG, TREE_OBJ) {
                        var user = $(NODE).find('a:first');
                        var name = user.text();
                        var data = {
                                id : $(NODE).attr('id'),
                                name : jQuery.trim(name)
                        }
                        $.post('/digUser/account/rename', data, function(response){
                            var newName = response.user.first_name + ' ' + response.user.last_name;
                                user.html('<ins></ins> ' + newName);
                }, 'json');
            }
    }

});
var initForm = function() {
    var formOptions = {
        target: '#main_form',
        success: function(){ 
        	// update the user's name in the tree
        	var user = $('#first_name').val() + ' ' + $('#last_name').val();
        	var userId = $('#id').val();
        	$("li.user a[rel=" + userId + "]").html("<ins></ins>" + user);
        	// move the item to the proper group
        	var userRole = $("#role").val();
                var currentRole = $("#" + userId).closest("li.role").attr("id");
                if(userRole != currentRole) {
                    var userNode = $("#" + userId);
                    var newParent = $("#" + userRole);
                    $.tree.focused().cut(userNode);
                    $.tree.focused().paste(newParent, 'inside');
                }
        	
            $('.messages').remove();
            $.dig.ui.init();
            initForm();
            $.dig.ui.enable();
        },
        beforeSubmit: function(){
            $.dig.ui.disable();
        }
    }
    $('.ajaxForm').ajaxForm(formOptions); 
}
$('a.test').click(function(){

});
<?php $this->jQuery()->onLoadCaptureEnd()?>
        

